/**
 */

package com.bei_en.common.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.xsshtmlfilter.HTMLFilter;
import org.apache.log4j.Logger;
import org.constretto.ConstrettoBuilder;
import org.constretto.ConstrettoConfiguration;
import org.constretto.model.Resource;

/**
 * @author Guocg
 *
 */
public class XssUtil {

	protected static Logger log = Logger.getLogger(XssUtil.class);
	public static final Map<String, Object> CONFIGURATION = Collections
			.unmodifiableMap(new HashMap<String, Object>() {

				{

					final ConstrettoConfiguration constretto = new ConstrettoBuilder()
							.createPropertiesStore()
							/*
							 * .addResource( Resource.create(
							 * "file:D:/amIurs/git/chezhidao-server/CheZhiDao/resources/xss.properties"
							 * ))
							 */
							.addResource(
									Resource.create("classpath:/xss.properties"))
							.done() // Multiple
							// resources
							// may
							// be
							// added
							.getConfiguration();
					final Map<String, List<String>> allowed = new HashMap<String, List<String>>();
					for (String allow : constretto.evaluateTo(
							"HTMLFilter.vAllowed", "").split("\\s*;\\s*")) {
						if (0 < allow.indexOf(':')) {
							final String name = allow.split("\\s*:\\s*")[0];
							final String[] attributes = allow
									.split("\\s*:\\s*")[1].split("\\s*,\\s*");
							allowed.put(name, Arrays.asList(attributes));
						} else {
							allowed.put(allow, Collections.<String> emptyList());
						}
					}
					put("vAllowed", allowed);
					put("vSelfClosingTags",
							constretto.evaluateTo(
									"HTMLFilter.vSelfClosingTags", "").split(
									"\\s*,\\s*"));
					put("vNeedClosingTags",
							constretto.evaluateTo(
									"HTMLFilter.vNeedClosingTags", "").split(
									"\\s*,\\s*"));
					put("vDisallowed",
							constretto.evaluateTo("HTMLFilter.vDisallowed", "")
									.split("\\s*,\\s*"));
					put("vAllowedProtocols",
							constretto.evaluateTo(
									"HTMLFilter.vAllowedProtocols", "").split(
									"\\s*,\\s*"));
					put("vProtocolAtts",
							constretto.evaluateTo("HTMLFilter.vProtocolAtts",
									"").split("\\s*,\\s*"));
					put("vRemoveBlanks",
							constretto.evaluateTo("HTMLFilter.vRemoveBlanks",
									"").split("\\s*,\\s*"));
					put("vAllowedEntities",
							constretto.evaluateTo(
									"HTMLFilter.vAllowedEntities", "").split(
									"\\s*,\\s*"));
					put("alwaysMakeTags", constretto.evaluateTo(Boolean.class,
							"HTMLFilter.alwaysMakeTags"));
					put("encodeQuotes", constretto.evaluateTo(Boolean.class,
							"HTMLFilter.encodeQuotes"));
				}
			});

	public static String handle(String input) {

		log.trace(CONFIGURATION);
		String clean = new HTMLFilter(CONFIGURATION).filter(input);
		// log.info("input="+input);
		// log.info("clean="+clean);

		return clean;
		// return input;
	}

	public static void main(String[] args) {

		String str = "&amp;<img src='sdf' height='df' sdf='sdf'><i>士大夫</i><s>dsf</s><script>aaa</script><sdf";
		// String str="{\'name\':\"收到\"}";
		System.out.println(handle(str));
		// new Gson().fromJson(handle(str), Object.class);
	}
}
